package orm;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.util.Log;

public class DBTest {
	
	public void runTest(Context context){

		Log.d("TESTS: ", "Testing ..");
		
		printAllPerson(context);
		printAllFitnessData(context);
		Person p = PersonDbHelper.getPersonByID(context, 1);
		printAllFitnessDataFromPerson(context, p);
		testExercise(context);
		testSet(context);
		
	}
	
	private void printAllPerson(Context context){
		Log.d("Get all Person: ", "Person ..");
		ArrayList<Person> personList = null;
		personList = PersonDbHelper.getAllPerson(context);
		for (Person person : personList) {
			Log.d("Person ID: ", ""+ person.getID());
			Log.d("Person Fname: ", person.getFName());
		}
		
		
	}
	
	private void printAllFitnessData(Context context){
		Log.d("Get all FitnessData: ", "FitnessData ..");
		ArrayList<FitnessData> fitnessDataList = null;
		fitnessDataList = FitnessDataDBHelper.getAllFitnessData(context);
		for (FitnessData fitnessData : fitnessDataList) {
			Log.d("FitnessData ID: ", ""+ fitnessData.getID());
			Log.d("FitnessData Weight: ", ""+ fitnessData.getWeight());
		}
		
		
	}
	
	private void printAllFitnessDataFromPerson(Context context, Person p){
		Log.d("Print all FitnessData from Person: ", "printing ..");
		ArrayList<FitnessData> fitnessDataList = null;
		fitnessDataList = FitnessDataDBHelper.getFitnessDataByPersonFK(context, p.getID());
		for (FitnessData fitnessData : fitnessDataList) {
			Log.d("FitnessData ID: ", ""+ fitnessData.getID());
			Log.d("FitnessData Weight: ", ""+ fitnessData.getWeight());
		}
		
		
	}
	
	
	private void testExercise(Context context){
		Date date = new Date();
		Exercise ex = new Exercise(0, 1, "PUSH_UPS", date);
		long id = ExerciseDBHelper.saveExercise(context, ex);
		Log.d("Save Exericse: " + id, "ID .." + ex.getExercise_id());
		Log.d("FitnessdataFK ", "FitnessdataFK .." + ex.getFitnessdata_fk());
		Log.d("NAME ", "NAME .." + ex.getExerciseName());
		Log.d("Date ", "Date .." + ex.getDate());
		
		Log.d("ID of new Exericse: ", "ID .." + id);
		
		Exercise ex_new = ExerciseDBHelper.getExerciseByID(context, id);
		
		Log.d("Load Exericse: " + id, "ID .." + ex_new.getExercise_id());
		Log.d("FitnessdataFK ", "FitnessdataFK .." + ex_new.getFitnessdata_fk());
		Log.d("NAME ", "NAME .." + ex_new.getExerciseName());
		Log.d("Date ", "Date .." + ex_new.getDate());
		
	}
	
	private void testSet(Context context){
		Date date = new Date();
		Log.d("Date test: ", "Date .." + date);
		Set set = new Set(0, 1, 12, date, 23, 1);
		long id = SetDBHelper.saveSet(context, set);
		
		Log.d("ID of new Set: ", "ID .." + id);
		
		Set set_new = SetDBHelper.getSetByID(context, id);
		
		Log.d("Load Set: " + id, "ID .." + set_new.getUnit_id());
		Log.d("Set Nr: ", "Setnr .." + set_new.getUnitNr());
		Log.d("Set Wiederholungen: ", "Wiederholungen .." + set_new.getValueInt());
		Log.d("Set Gewicht: ", "Gewicht .." + set_new.getValueWeight());
		Log.d("Set Zeit: ", "Zeit .." + set_new.getValueTime());
		Log.d("Exerciser FK ", "FK .." + set_new.getExercise_fk());
		
	}

	/**
	 * Generates testing data 
	 */
	public Person generatTestData(Person person) {
		
			//check if testdata already exists
		if(person.getFName() != null && person.getFName().equalsIgnoreCase("Max")) return person;
		
		person.setDateOfBirth(new Date());
		
			
		return person;
		
	}

}
